﻿using System;
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;
using ShootSearch.Indexers;
using ShootSearch.Util;
using ShootSearch.Util.Serializers;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Data.Common;

namespace ShootSearch.Tests.Indexers
{
    [TestFixture]
    public class DBIndexerTests
    {
        [Test]
        public void ConfigTest()
        {
            DBIndexerConfig expected = new DBIndexerConfig();
            expected.ConnectionString = "Data Source=127.0.0.1;Initial Catalog=News;";
            expected.Connection = new AssemblyConfig(typeof(SqlConnection));
            expected.Insert = "Insert {0} ({1}) Values({2});";
            expected.TableCheck = string.Empty;
            expected.TableCreation = string.Empty;
            expected.Table = "News";
            expected.UniqueNameSpliter = "[{0}]";
            expected.InterestedAttributes.Add(new DBIndexerConfig.DBIndexerAttribute("Title", System.Data.DbType.String, true));
            expected.InterestedAttributes.Add(new DBIndexerConfig.DBIndexerAttribute("Content", System.Data.DbType.String, true));
            expected.InterestedAttributes.Add(new DBIndexerConfig.DBIndexerAttribute("Time", System.Data.DbType.DateTime, true,"yyyy-MM-dd HH:mm:ss"));
            expected.InterestedAttributes.Add(new DBIndexerConfig.DBIndexerAttribute("Url", System.Data.DbType.String, true));

            string xml = XmlFormatter.ToXml(expected);

            Debug.WriteLine(xml);
        }


        public void ConnectionTest()
        {
            string ConnectionString = "Data Source=127.0.0.1;Initial Catalog=News;";
            string sqlserver = "Data Source=(local);Initial Catalog=News;Uid=sa;Pwd=asdf;";

            Type type = typeof(SqlConnection);
            string typestring = type.FullName + "," + type.Assembly.FullName;
            Console.WriteLine(typestring);

            Type act = Type.GetType(typestring);

            Assert.AreNotEqual(null, act);


            //string newTypestr = "System.Data.SqlClient.SqlConnection,System.Data";
            //act = Type.GetType(newTypestr);

            //Assert.AreNotEqual(null, act);
            ////string connType = typeof(SqlConnection).ToString();
            //string connType = typeof(Hubble.SQLClient.SqlConnection).ToString();

            //Type tp = Type.GetType(connType, false, true);

            //Assert.AreNotEqual(null, tp, "Type Failed!" + connType);

            //object obj = tp.Assembly.CreateInstance(connType);

            //Assert.AreNotEqual(null, obj);

            //Type type = typeof(SqlConnection);

            //Type newType = Activator.CreateInstance(type);

            //Assert.AreEqual(type.FullName, newType.FullName);

            //DbProviderFactory factory = new Sql
            
            //DbConnectionStringBuilder builder = SqlClientFactory.Instance.CreateConnectionStringBuilder();
            AssemblyConfig ass = new AssemblyConfig(typeof(SqlConnection));
            Debug.WriteLine(XmlFormatter.ToXml(ass));
            //Hubble.SQLClient.SqlConnection conn 
            //AssemblyConfig ass = new AssemblyConfig(typeof(SqlConnection));
            object objConn = ass.GetObject(ConnectionString);
            DbConnection dbc = objConn as DbConnection;
            Assert.AreNotEqual(null, objConn); 
            Assert.AreNotEqual(null, dbc);


            //Hubble.SQLClient.SqlConnection conn = new Hubble.SQLClient.SqlConnection(
        }

        [Test]
        public void HubbleTest()
        {
         
            
            
        }

    }
}
